<template>
  <div id="tbooking-policies">
    <nav aria-label="breadcrumb" role="navigation">
        <ol class="breadcrumb pb-1">
          <li class="breadcrumb-item">携程政策-自动比价</li>
          <span class="ms-auto" @click.stop="back()">返回</span>
        </ol>
    </nav>        
    <div class="card">
      <div class="card-body small ">
        <form class="" id="frmSearchPolicy">
            <div class="col-1">
                <select class="form-select" v-model="policyType">
                    <option value="1" selected>特殊政策</option>
                </select>
            </div>
            <div class="col-1">
                <select class="form-select" v-model="monitoring">
                    <option value="-1" selected>全部</option>
                    <option value="0">未监控</option>
                    <option value="1">监控中</option>
                </select>
            </div>
            <input type="textfield" class="form-control" placeholder="航司两字代码" size="10" v-model.trim="carrier">
            <input type="textfield" class="form-control" placeholder="政策代码" size="10" v-model.trim="policyCode">
            <my-date-picker id="tbookingPolicyDdate" placeholder="出发日期" v-model.trim="ddate"></my-date-picker>
            <div class="col-1">
                <button type="button" class="btn btn-primary ms-2" @click.stop="search()">查找</button>
				        <button type="button" class="btn btn-secondary btn-sm ms-2" @click.stop="reset()">重置</button>

                <button type="button" class="btn btn-secondary btn-sm ms-5" @click.stop="syncPolicy()">同步</button>
            </div>
        
          <button type="button" class="btn btn-danger btn-sm ms-auto" @click.stop="deleteAll()">全部删除</button>

          <router-link to="/tbooking/policy/bottom-price" class="btn btn-success ms-auto">底价管理</router-link>
        </form>      	
      </div>
    </div>
    <table class="table table-sm table-striped table-hover small">
      <thead>
  	    <tr>
  	        <th></th>
  	        <th>政策代码</th>
  	        <th>航空公司</th>
  	        <th>出发机场</th>
  	        <th>到达机场</th>
  	        <th>航班开始日期</th>
  	        <th>航班截至日期</th>
  	        <th>去程航班</th>
  	        <th>去程舱位</th>
  	        <th>结算价</th>
  	        <th>监控时间</th>
  	        <th>注释</th>
  	        <th></th>
  	    </tr>
  	  </thead>
	    <tbody>
        <template v-for="info in dataList">
      <tr :key="info.id">
        <td>
          <button type="button" @click.stop="localDelete(info.policyId, policyType)">本地删除</button>
        </td>
        <td>{{info.policyCode}}</td>
        <td>
            {{info.carrier}}</a>
        </td>
        <td>{{info.departurePort}}</td>
        <td>{{info.arrivalPort}}</td>
        <td>{{info.goFlightEffectDate}}</td>
        <td>{{info.goFlightExpiryDate}}</td>
        <td>
            {{info.goFlightNo}}
        </td>
        <td>
          <template v-if="info.ignoreSubclass === 1">
            */{{info.cabinClass}}
          </template>
          <template v-else>
            {{info.goSubClass}}/{{info.cabinClass}}
          </template>            
        </td>
        <td>
            {{info.setPrice}}</a>
        </td>
        <td>{{info.monitorTime}}</td>
        <td>{{info.remark}}</td>
        <td>
            <template v-if="info.monitoring">
              <!-- <a :href="monitorPolicy(0, info.policyId,info.policyType);"> -->
                    监控中<button type="button" class="btn btn-sm" @click.stop="stopMonitorPolicy(info.policyId,1)">停止</button>
<!--                 </a>              
 -->            </template>
            <template v-else>
              <!-- <a :href=";"> -->
                <button type="button" class="btn btn-sm btn-success" @click.stop="startMonitorPolicy(info.policyId,1)">开始监控</button>
                <!-- </a>               -->
            </template>
        </td>
        </tr>      
      </template>
    </tbody>
    </table>
          
    <nav id="pagination-box" class="float-end">
      <my-pagination name='pagination' :row-count='sc.rowCount' :page-total='sc.pageTotal' :page-no='sc.pageNo' @next-page='nextPage' @prev-page='prevPage' @direct-page='directPage'></my-pagination> 
    </nav>

    <my-modal-prompt ref="modalPrompt" :nullable="modalNullable">
      <span slot="title">{{modalTitle}}</span>
    </my-modal-prompt>
  </div>
</template>

<script>
import {
  searchSpecialPolicies,
  startMonitorPolicy,
  stopMonitorPolicy,
  syncPolicy,
  localDeletePolicy,
  deleteAllLocalPolicy,
} from "@/api/tbooking.js";
import MyPagination from "@/components/my-pagination.vue";
import MyModalPrompt from "@/components/modal/my-modal-prompt.vue";
import MyDatePicker from "@/components/my-datepicker.vue";

export default {
  components: {
    MyDatePicker,
    MyPagination,
    MyModalPrompt,
  },
  data() {
    return {
      dataList: [],

      sc: {
        rowCount: 0,
        pageNo: 1,
        pageSize: 20,
        pageTotal: 0,
      },

      policyType: 1,
      monitoring: -1,

      beginDate: "",
      endDate: "",
      status: -1,
      orderId: "",
      policyCode: "",
      carrier: "",
      policyCode: "",
      ddate: "",

      modalTitle: "",
      modalNullable: false,
    };
  },
  mounted: function () {
    this.search();
  },
  methods: {
    back: function () {
      this.$router.go(-1);
    },
    showErrMsg: function (msg, msgType) {
      this.$store.dispatch("showAlertMsg", {
        errMsg: msg,
        errMsgType: msgType,
      });
    },
    showLoading: function (loadingText) {
      this.$store.commit("showLoading", {
        loading: true,
        loadingText: loadingText,
      });
    },
    hideLoading: function () {
      this.$store.commit("showLoading", { loading: false });
    },
    search: function () {
      let y = 0;
      const x = parseInt(this.orderId);
      if (isNaN(x) === false) {
        y = x;
      }
      const params = {
        "sc.pageNo": this.sc.pageNo,
        "sc.pageSize": this.sc.pageSize,
        "sc.beginDate": this.beginDate,
        "sc.endDate": this.endDate,
        "sc.status": this.status,
        "sc.orderId": y,
        "sc.policyCode": this.policyCode,
        "sc.policyType": this.policyType,
        "sc.monitoring": this.monitoring,
        "sc.carrier": this.carrier,
        "sc.policyCode": this.policyCode,
        "sc.ddate": this.ddate,
      };

      this.showLoading();

      searchSpecialPolicies(
        params,
        (jsonResult) => {
          this.dataList = jsonResult.dataList;
          this.sc = jsonResult.page;
        },
        () => {
          this.hideLoading();
        }
      );
    },
    startMonitorPolicy: function (policyId, policyType) {
      this.modalTitle = "比价时忽略具体舱位?";
      this.$refs.modalPrompt
        .modal("YesOrNo")
        .then(() => {
          this.doStartMonitorPolicy(policyId, policyType, 1);
        })
        .catch((ex) => {
          this.doStartMonitorPolicy(policyId, policyType, 0);
        });
    },
    doStartMonitorPolicy: function (policyId, policyType, ignoreSubclass) {
      const params = {
        policyId,
        policyType,
        ignoreSubclass,
      };

      this.showLoading();

      startMonitorPolicy(
        params,
        (v) => {
          this.search();
        },
        () => {
          this.hideLoading();
        }
      );
    },
    stopMonitorPolicy: function (policyId, policyType) {
      const params = {
        policyId,
        policyType,
      };

      this.showLoading();

      stopMonitorPolicy(
        params,
        (v) => {
          this.search();
        },
        () => {
          this.hideLoading();
        }
      );
    },
    syncPolicy: function () {
      const params = {
        policyType: this.policyType,
        "sc.carrier": this.carrier,
        "sc.policyCode": this.policyCode,
      };

      this.showLoading();
      syncPolicy(
        params,
        (v) => {
          console.log(v);
          if (v.status === "OK") {
            this.showErrMsg("同步成功");
            this.search();
          } else {
            this.showErrMsg(v.errmsg, "danger");
          }
        },
        () => {
          this.hideLoading();
        }
      );
    },
    localDelete: function (policyId, policyType) {
      const params = {
        policyId,
        policyType,
      };

      this.showLoading();

      localDeletePolicy(
        params,
        (v) => {
          this.search();
        },
        () => {
          this.hideLoading();
        }
      );
    },
    deleteAll: function () {
      this.modalTitle = "全部删除?";
      this.$refs.modalPrompt
        .modal("YesOrNo")
        .then(() => {
          deleteAllLocalPolicy((v) => {
            console.log(v);
            if (v.status === "OK") {
              this.showErrMsg("删除成功");
              this.search();
            }
          });
        })
        .catch((ex) => {});
    },
    getOrderStatus: function (status) {
      switch (status) {
        case "1":
          return "未出票";
        case "2":
          return "自动出票失败";
        case "3":
          return "出票成功";
        case "4":
          return "已取消";
        case "5":
          return "已作废";
        case "6":
          return "已入库";
        case "7":
          return "已支付";
        case "8":
          return "验证中";
        default:
          return status;
      }
    },
    reset: function () {
      this.status = -1;
      this.sc.pageNo = 1;
      this.beginDate = "";
      this.endDate = "";
      this.orderId = "";
      this.policyCode = "";
      this.carrier = "";
      this.ddate = "";
    },
    prevPage: function () {
      this.sc.pageNo = this.sc.pageNo - 1;
      if (this.sc.pageNo < 1) this.sc.pageNo = 1;
      this.search();
    },
    nextPage: function () {
      this.sc.pageNo = this.sc.pageNo + 1;
      this.search();
    },
    directPage: function (pageNo) {
      this.sc.pageNo = pageNo;
      this.search();
    },
  },
};
</script>